<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="all">
      <el-card>
        <el-row>
          <el-button type="primary" class="btn" @click="showAdd"
            >添加用户</el-button
          >
        </el-row>
        <tree-table
          :data="list"
          :columns="columns"
          :selection-type="false"
          :expand-type="false"
          show-index
          index-text="#"
          border=""
        >
          <template #isok="{ row }">
            <i
              v-if="row.cat_deleted === false"
              class="el-icon-success"
              style="color: lightgreen"
            ></i>
            <i class="el-icon-erroe" style="color: red" v-else></i>
          </template>
          <template #order="{ row }">
            <el-tag v-if="row.cat_level === 0">一级</el-tag>
            <el-tag v-else-if="row.cat_level === 1" type="success">二级</el-tag>
            <el-tag v-else type="warning">三级</el-tag>
          </template>
          <template #opt="{}">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete">删除</el-button>
          </template>
        </tree-table>
      </el-card>
    </div>
    <el-pagination
      class="fooer"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="userinfo.pagenum"
      :page-sizes="[1, 2, 3, 5]"
      :page-size="userinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      @close="addcateclose"
      title="添加分类"
      :visible.sync="categoriesshow"
      width="50%"
    >
      <el-form
        :model="addcatform"
        :rules="Addrules"
        ref="ruleForm1"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addcatform.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader
            v-model="selectkey"
            :props="cascaderProps"
            :options="parentCateList"
            @change="handleChange"
            clearable
            change-on-select
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="categoriesshow = false">取 消</el-button>
        <el-button type="primary" @click="addcat">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addjiekou, getCategories, getcategoriesList } from "@/api/cat";

export default {
  data() {
    return {
      selectkey: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      parentCateList: [],
      ruleForm: {},
      Addrules: {
        cat_name: {
          required: true,
          message: "请输入分类名称",
          trigger: "blur",
        },
      },
      addcatform: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      categoriesshow: false,
      list: [],
      userinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 2,
      },
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "opt",
        },
      ],
    };
  },
  created() {
    this.getcategoriesallList();
  },
  methods: {
    showAdd() {
      this.getparentCataList();
      this.categoriesshow = true;
    },
    async getcategoriesallList() {
      const res = await getcategoriesList(this.userinfo);
      console.log(res);
      this.list = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(e) {
      // console.log(e)
      this.userinfo.pagesize = e;
      this.getcategoriesallList();
    },

    handleCurrentChange(r) {
      // console.log(r)
      this.userinfo.pagenum = r;
      this.getcategoriesallList();
    },
    async getparentCataList() {
      const res = await getCategories();
      // console.log(res)
      this.parentCateList = res.data;
    },
    handleChange() {
      // console.log(this.selectkey)
      if (this.selectkey.length > 0) {
        this.addcatform.cat_pid = this.selectkey[this.selectkey.length - 1];
        this.addcatform.cat_level = this.selectkey.length;
        return;
      } else {
        this.addcatform.cat_pid = 0;
        this.addcatform.cat_level = 0;
      }
    },
    async addcat() {
      // console.log(this.addcatform)
      await this.$refs.ruleForm1.validate();
      const res = await addjiekou(this.addcatform);
      console.log(res);
      if (res.meta.status !== 201) return this.$message.error("添加失败");
      this.$message.success("添加成功");
      this.getcategoriesallList();
      this.categoriesshow = false;
    },
    addcateclose() {
      this.$refs.ruleForm1.resetFields();
      this.selectkey = [];
      this.addcatform.cat_level = 0;
      this.addcatform.pid = 0;
    },
  },
};
</script>
<style lang="scss" scoped>
.all {
  margin-top: 20px;
}
.el-cascader {
  width: 100%;
}
</style>
